package cn.stylefeng.guns.modular.comm.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.comm.entity.WxUser;
import cn.stylefeng.guns.modular.comm.mapper.WxUserMapper;
import cn.stylefeng.guns.modular.comm.model.params.WxUserParam;
import cn.stylefeng.guns.modular.comm.model.result.WxUserResult;
import  cn.stylefeng.guns.modular.comm.service.WxUserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 微信用户信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-01-30
 */
@Service
public class WxUserServiceImpl extends ServiceImpl<WxUserMapper, WxUser> implements WxUserService {

    @Override
    public void add(WxUserParam param){
        WxUser entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(WxUserParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(WxUserParam param){
        WxUser oldEntity = getOldEntity(param);
        WxUser newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public WxUserResult findBySpec(WxUserParam param){
        return null;
    }

    @Override
    public List<WxUserResult> findListBySpec(WxUserParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(WxUserParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(WxUserParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private WxUser getOldEntity(WxUserParam param) {
        return this.getById(getKey(param));
    }

    private WxUser getEntity(WxUserParam param) {
        WxUser entity = new WxUser();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
